package work.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.entity.Information;
import work.service.InformationService;
import work.vo.DataVo;

import java.util.List;

@RestController
@RequestMapping("/information")
public class InformationHandler {

    @Autowired
    private InformationService informationService;

    // 读者查看管理员消息
    @PostMapping("/getInformation")
    public DataVo getInformation(@RequestBody String userId) {
        DataVo dataVo = new DataVo();

        List<Information> informationList = informationService.findInformation(userId);
        if (informationList.isEmpty()) {
            dataVo.setCode(200);
            dataVo.setMsg("目前没有发送给您的消息");
        } else {
            dataVo.setCode(201);
            dataVo.setMsg("存在发送给您的消息");
            dataVo.setData(informationList);
        }
        return dataVo;
    }

    // 工作人员发送消息给读者
    // 前端传入一个information对象，userId和informationText不为空
    @PostMapping("/sendInformation")
    public DataVo sendInformation(@RequestBody Information information) {
        DataVo dataVo = new DataVo();

        information.setInformationId(String.valueOf(System.currentTimeMillis()));
        int i = informationService.insertInformation(information);

        if (i == 0) {
            dataVo.setCode(380);
            dataVo.setMsg("消息发送失败");
        } else {
            dataVo.setCode(381);
            dataVo.setMsg("消息发送成功");
        }

        return dataVo;
    }
}